package com.lu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lu.model.dto.UserRoleResourcesDTO;
import com.lu.model.entity.SysUser;
import com.lu.model.request.*;
import com.lu.model.response.Result;
import com.lu.service.SysUserService;
import com.lu.utils.SecurityUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 *
 *
 * </p>
 *
 * @author zhanglu
 * @since 2022-07-26
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys-user")
public class SysUserController {

  private final SysUserService userService;

  /**
   * 用户登出
   * @return
   */
  @PostMapping("/logout")
  public Result login(){
    userService.logout();
    return Result.success();
  }

  /**
   * 查询用户角色资源信息
   * @return 当前用户资源列表详情，以便前端隐藏，url是与前端约定好的
   */
  @GetMapping("/menu/role-resources")
  public Result<List<UserRoleResourcesDTO.TreeNodes>> getUserRoleMenuResources(){
    return Result.success(userService.getUserRoleMenuResources());
  }

  /**
   * 查询用户角色资源信息
   * @return 当前用户资源列表详情，以便前端隐藏，url是与前端约定好的
   */
  @GetMapping("/button/role-resources")
  public Result<List<UserRoleResourcesDTO>> getUserRoleButtonResources(){
    return Result.success(userService.getUserRoleButtonResources());
  }

  /**
   * 添加用户
   * @param request
   * @return 用户id
   */
  @PreAuthorize("hasAuthority('/sys-user/add')")
  @PostMapping(value = "/add")
  public Result<Long> add(@RequestBody @Validated UserAddRequest request){
    return Result.success(userService.addUser(request));
  }

  /**
   * 修改
   * @param request
   * @return 最新的用户信息
   */
  @PreAuthorize("hasAuthority('/sys-user/update')")
  @PostMapping(value = "/update")
  public Result<SysUser> update(@RequestBody @Validated UserUpdateRequest request){
    return Result.success(userService.updateUser(request));
  }

  /**
   * 修改-个人信息
   * @param request
   * @return 最新的用户信息
   */
  @PostMapping(value = "/update-info")
  public Result<SysUser> updateInfo(@RequestBody UserUpdateRequest request){
    request.setUserId(SecurityUtil.getCurrentUser().getId());
    return Result.success(userService.updateUser(request));
  }

  /**
   * 修改密码
   * @param request
   * @return 用户id
   */
  @PostMapping(value = "/update-pwd")
  public Result<Integer> updatePwd(@RequestBody @Validated UserUpdatePwdRequest request){
    return Result.success(userService.updatePwd(request));
  }

  /**
   * 更新用户状态
   * @param id
   * @param status
   * @return 用户id
   */
  @PreAuthorize("hasAuthority('/sys-user/update-status')")
  @PostMapping(value = "/update-status/{id}/{status}")
  public Result<Integer> updateStatus(@PathVariable Long id, @PathVariable Integer status){
    return Result.success(userService.updateStatus(id, status));
  }

  /**
   * 更新用户角色关系
   * @param request
   * @return 用户id
   */
  @PreAuthorize("hasAuthority('/sys-user/update-roles')")
  @PostMapping(value = "/update-roles")
  public Result<String> updateRoles(@RequestBody @Validated UserRolesUpdateRequest request){
    return Result.success(userService.updateRoles(request));
  }

  /**
   * 删除用户
   * @param id
   * @return 用户id
   */
  @PreAuthorize("hasAuthority('/sys-user/delete')")
  @PostMapping(value = "/delete/{id}")
  public Result<String> deleteUser(@PathVariable Long id){
    return Result.success(userService.deleteUser(id));
  }

  /**
   * 用户分页查询
   * @param request
   * @return
   */
  @PreAuthorize("hasAuthority('/sys-user/page-list')")
  @RequestMapping(value = "/page-list")
  public Result<Page<SysUser>> pageList(@RequestBody UserQueryRequest request){
    Page<SysUser> userPage = userService.pageList(request);
    return Result.success(userPage);
  }

  /**
   * 用户详情
   * @param id
   * @return
   */
  @PreAuthorize("hasAuthority('/sys-user/get')")
  @GetMapping(value = "/get/{id}")
  public Result<SysUser> getById(@PathVariable Long id){
    return Result.success(userService.getByUserId(id));
  }

}
